<?php

namespace App\Http\Controllers\Permission;

use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $users=User::where('is_admin',1)->get();
        return view('permission.user.index',compact('users'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
     return view('permission.user.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
//        dd($request->is_admin);
        if (filter_var($request->account,FILTER_VALIDATE_EMAIL)){
            $data['email']=$request->account;
            $dat['email_valid']=true;
        }else{
            $data['phone']=$request->account;

            $dat['mobile_valid']=true;
        }
        $data['name']=$request->name;
        $data['password']=bcrypt($request->password);
    //dump($data);die;
        if (isset($request->is_admin)){
            $data['is_admin']=1;
        }
        User::create($data);
        return redirect(route('permission.user.index'))->with('success','添加用户成功');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(User $user)
    {

        return view('permission.user.edit',compact('user'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, User $user)
    {
//      dd($request->toArray());
        if (filter_var($request->account,FILTER_VALIDATE_EMAIL)){
            $data['email']=$request->account;
            $dat['email_valid']=true;
        }else{
            $data['phone']=$request->account;

            $dat['mobile_valid']=true;
        }
        $data['name']=$request->name;
        $data['password']=bcrypt($request->password);

        if (isset($request->is_admin)){
            $data['is_admin']=1;
        }else{
            $data['is_admin']=0;
        }
        $user->update($data);
//       dd( $user->update($data));


        return redirect(route('permission.user.index'))->with('success','编辑用户成功');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(User $user)
    {
        dd($user);
    }
}
